package com.it.myh;

/**
 * @Description: // 类说明，在创建类时要填写
 * @ClassName: Ev_0216_04    // 类名，会自动填充
 * @Author: MYH          // 创建者
 * @Date: 2023/2/16 15:48   // 时间
 * @Version: 1.0     // 版本
 */
public class Ev_0216_04 {
    public static void main(String[] args) {

        StringBuffer s1 = new StringBuffer();
        StringBuffer s2 = new StringBuffer();
        new Thread(){
            @Override
            public void run() {
                synchronized (s1){
                    s1.append("a");
                    s2.append(1);

                    synchronized (s2){
                        s1.append("b");
                        s2.append(2);
                        System.out.println(s1);
                        System.out.println(s2);
                    }
                }
            }
        }.start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (s2){
                    s1.append("c");
                    s2.append(3);

                    synchronized (s1){
                        s1.append("d");
                        s2.append(4);
                        System.out.println(s1);
                        System.out.println(s2);
                    }
                }
            }
        }).start();
    }
}
